package com.xplay.xpocker.util;

import com.xplay.xpocker.constant.ConstantDict;
import com.xplay.xpocker.dvo.GameRuMahjongUserState;
import com.xplay.xpocker.entity.GameRuMahjongOpenDeal;
import com.xplay.xpocker.room.mahjong.util.MahjongHuUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wanjie
 * @date 2021/3/10 20:31
 */

public class CardUtil {

    private static final Integer CARD_TYPE_WAN = 0;
    private static final Integer CARD_TYPE_TIAO = 1;
    private static final Integer CARD_TYPE_TONG = 2;

    public static final String ACTION_COVER = "被";

    public static final String ACTION_BY_OWN = "自摸";

    public static final String ACTION_SHOOTING = "放炮";

    public static final String ACTION_CALLING = "查叫";

    public static final String ACTION_CLAIM = "赔叫";

    public static final String ACTION_BAR = "杠";

    public static final String ACTION_MING_BAR = "明杠";

    public static final String ACTION_AN_BAR = "暗杠";

    public static final String ACTION_DIAN_BAR = "点杠";

    public static final String ACTION_RECEIVE = "接炮";

    public static final String PING_HU = "平胡";
    public static final String DA_DUI = "大队子";
    public static final String JIN_GOU = "金钩钩";
    public static final String QI_DUI = "七对";
    public static final String QING_YI_SHE = "清一色";
    public static final String QING_DA_DUI = "清大对";
    public static final String QING_JIN_GOU = "清金钩";
    public static final String QING_QI_DUI = "清七对";


    /**
     * 倍数
     */
    public static final Integer ZERO = 0;
    public static final Integer ONE = 1;
    public static final Integer DOUBLE = 2;
    public static final Integer TRIPLE = 3;
    public static final Integer FOURFOLD = 4;
    public static final Integer QUINTUPLE = 5;
    public static final Integer SIXFOLD = 6;
    public static final Integer SEVENFOLD = 7;
    public static final Integer EIGHTFOLD = 8;
    public static final Integer NINEFOLD = 9;


    /**
     * 从用户的手牌中移除指定的牌
     */
    public static void removeCard(List<Integer> userCards, Integer exportCard) {
        int userCardSize = userCards.size();
        for (int i = userCardSize - 1; i >= 0; i--) {
            if (userCards.get(i).equals(exportCard)) {
                userCards.remove(i);
                return;
            }
        }
    }


    public static void cardByGroup(Map<Integer, LinkedList<Integer>> cardGroup, Integer val, Integer type) {
        if (cardGroup.get(type) == null) {
            LinkedList<Integer> card = new LinkedList<>();
            card.add(val);
            cardGroup.put(type, card);
        } else {
            cardGroup.get(type).add(val);
        }
    }

    /**
     * 判断卡牌 是否是同色
     *
     * @param allCard
     * @return
     */


    public static int judgeUserCardClass(List<Integer> allCard) {
        Integer wanCount = allCard.stream().anyMatch(n -> n >= 1 && n <= 10) ? 1 : 0;
        Integer tioCount = allCard.stream().anyMatch(n -> n >= 11 && n <= 20) ? 1 : 0;
        Integer tongCount = allCard.stream().anyMatch(n -> n >= 21 && n <= 30) ? 1 : 0;
        return wanCount + tioCount + tongCount;
    }


    public static String[] getCard(String[] card, int count) {
        String[] newCardArray = new String[count];
        System.arraycopy(card, card.length - count, newCardArray, 0, count);
        return newCardArray;
    }

    public static String[] delCard(String[] card, int count) {
        int resultCount = card.length - count;
        String[] newCardArray = new String[resultCount];
        System.arraycopy(card, 0, newCardArray, 0, resultCount);
        return newCardArray;
    }

    public static String joinCard(String cards[]) {
        if (cards == null || cards.length == 0) {
            return "";
        }
        StringBuilder cardBuilder = new StringBuilder();
        for (String num : cards) {
            cardBuilder.append(num);
            cardBuilder.append(",");
        }
        cardBuilder.setLength(cardBuilder.length() - 1);
        return cardBuilder.toString();
    }

    public static String joinCard(List<Integer> cards) {
        if (cards == null || cards.size() == 0) {
            return "";
        }
        StringBuilder cardBuilder = new StringBuilder();
        for (Integer num : cards) {
            cardBuilder.append(num);
            cardBuilder.append(",");
        }
        cardBuilder.setLength(cardBuilder.length() - 1);
        return cardBuilder.toString();
    }


    public static String buildAllCardString() {
        String nums[] = new String[108]; // 给定的数字
        int index = 0;
        for (int m = 3; m > 0; m--) {
            for (int i = 9; i > 0; i--) {
                for (int j = 1; j <= 4; j++) {
                    int head = (m - 1);
                    String code = (head > 0 ? head : "") + "" + i;
                    nums[index] = code;
                    index++;
                }
            }
        }
        shuffleArray(nums);
        return joinCard(nums);
    }


    public static void shuffleArray(String[] arr) {
        Random rand = new Random();
        for (int i = arr.length - 1; i > 0; i--) {
            int j = rand.nextInt(i + 1);
            String temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    public static String[] buildCard(String cards) {
        if (cards == null || cards.equals("")) {
            return new String[]{};
        }
        return cards.split(ConstantDict.Separator.comma);
    }

    public static List<Integer> buildCardList(String cards) {
        return Arrays.stream(cards.split(ConstantDict.Separator.comma)).map(Integer::valueOf).collect(Collectors.toList());
    }
}
